//
//  ProductController.m
//  ProductDemo
//
//  Created by hoho on 14-11-7.
//  Copyright (c) 2014年 hoho. All rights reserved.
//

#import "ProductController.h"
#import "ProductListController.h"

@interface ProductController ()

@end

static const NSUInteger selectedIndex = 0; //初始选中哪个tab

@implementation ProductController {
    UIView *contentContainerView;
    NSMutableArray *childViewControllers;
    UIViewController *currentChildViewController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    //添加内容容器
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    CGFloat barHeight = self.navigationController.navigationBar.frame.size.height;
    CGRect rect = CGRectMake(0, barHeight, self.view.bounds.size.width, self.view.bounds.size.height-barHeight-_tabBar.frame.size.height);
    rect.origin.y = barHeight;
    contentContainerView = [[UIView alloc] initWithFrame:rect];
    contentContainerView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:contentContainerView];
    //初始化childViewController数组
    childViewControllers = [[NSMutableArray alloc]init];
    NSUInteger tabItemsCount = _tabBar.items.count;
    for(NSUInteger i=0;i<tabItemsCount;i++){
        [childViewControllers addObject:[NSNull null]];
    }
    //初始选中一个tab
    UITabBarItem *selectedTab = [_tabBar.items objectAtIndex:selectedIndex];
    [_tabBar setSelectedItem:selectedTab];
    [self setCurrentChildViewController:selectedTab];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setCurrentChildViewController :(UITabBarItem *)item{
    
    NSUInteger index = [[_tabBar items] indexOfObject:item];
    NSUInteger currentIndex = [childViewControllers indexOfObject:currentChildViewController];
    if(index == currentIndex){ //不要重复点它
        return;
    }
    UIViewController *newViewController;
    id obj = [childViewControllers objectAtIndex:index];
    if(obj == [NSNull null]) { //创建childViewController
        
        UIStoryboard* mainStoryboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
        ProductListController *controller = [mainStoryboard instantiateViewControllerWithIdentifier:@"ProductList"];
        controller.cid = item.tag;
        
        [self addChildViewController:controller];
        
        [childViewControllers replaceObjectAtIndex:index withObject:controller];
        newViewController = controller;
        
    } else { //已创建了
        newViewController = obj;
    }
    newViewController.view.frame = contentContainerView.bounds;
    if(currentChildViewController == nil) { //当前还没有childViewController,直接显示新的
        [contentContainerView addSubview:newViewController.view];
    } else { //当前已有childViewController，动画切换为新的
        CGRect rect = contentContainerView.bounds;
        if (currentIndex < index) {
            rect.origin.x = rect.size.width;
        } else {
            rect.origin.x = -rect.size.width;
        }
        newViewController.view.frame = rect;
        [self transitionFromViewController:currentChildViewController
                          toViewController:newViewController
                                  duration:0.3
                                   options:UIViewAnimationOptionLayoutSubviews | UIViewAnimationOptionCurveEaseOut
                                animations:^{
                                    CGRect rect = currentChildViewController.view.frame;
                                    if (currentIndex < index) {
                                        rect.origin.x = -rect.size.width;
                                    } else {
                                        rect.origin.x = rect.size.width;
                                    }
                                    currentChildViewController.view.frame = rect;
                                    newViewController.view.frame = contentContainerView.bounds;
                                }
                                completion:^(BOOL finished) {}
         ];
    }
    currentChildViewController = newViewController;
}


- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
    [self setCurrentChildViewController:item];
}

@end
